package com.hjwoo.app.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.Button;

import com.hjwoo.app.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by yangchuan on 2016/5/20.
 */
public class VCodeButton extends Button
{
    private Timer   timer;
    private Handler handler;
    private int     count;

    public VCodeButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    private void init()
    {
        setBackgroundResource(R.drawable.vcode_btn_orange);
        setTextColor(getResources().getColor(R.color.white));
        setText("获取验证码");
        setTextSize(12);
        setPadding(15, 8, 15, 8);
        handler = new Handler()
        {
            public void handleMessage(Message msg)
            {
                if (msg.what == 0)
                {
                    setEnabled(true);
//                    setBackgroundColor(Color.parseColor("#FF8C00"));
                    setBackgroundResource(R.drawable.vcode_btn_orange);
                    setTextColor(Color.WHITE);
                    setText("重新获取");
                    timer.cancel();
                }
                else
                {
//                    setBackgroundColor(Color.parseColor("#DCDCDC"));
                    setBackgroundResource(R.drawable.vcode_btn_gry);
                    setTextColor(Color.parseColor("#808080"));
                    setText(msg.what + "s 后重发");
                }
            }

        };
    }

    public void startCount()
    {
        count = 60;
        setEnabled(false);
        timer = new Timer();
        timer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                handler.sendEmptyMessage(count--);
            }
        }, 0, 1000);
    }
}
 
